<?php
/**
 * @title 2719. 统计整数数目 - 数位DP优化
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $num1
     * @param String $num2
     * @param Integer $min_sum
     * @param Integer $max_sum
     * @return Integer
     */
    function count($num1, $num2, $min_sum, $max_sum) {
        /**
         * @since 2024-01-17 避免反复遍历
         */
        $this->dp = [];

        /**
         * @since 2024-01-17 num1补足前导0
         */
        $num1 = str_repeat("0", strlen($num2) - strlen($num1)) . $num1;

        /**
         * @since 2024-01-17 [num1, num2]的好整数个数
         */
        return $this->GoodCount($num1, $num2, 0, true, true, 0, $min_sum, $max_sum);
    }

    /**
     * @title [num1, num2]的好整数个数
     * @author start2004
     * @since 2024-01-17 Created
     *
     * @param String $num1
     * @param String $num2
     * @param Integer $i
     * @param Boolean $lowLimit
     * @param Boolean $highLimit
     * @param Integer $sum
     * @param Integer $minSum
     * @param Integer $maxSum
     * @return Integer
     */
    function GoodCount($num1, $num2, $i, $lowLimit, $highLimit, $sum, $minSum, $maxSum){
        /**
         * @since 2024-01-17 sum是递增的
         */
        if($sum > $maxSum){
            return 0;
        } else {}

        /**
         * @since 2024-01-17 递归终止条件
         */
        if($i == strlen($num1)){
            if($sum >= $minSum && $sum <= $maxSum){
                return 1;
            } else {
                return 0;
            }
        } else {}

        /**
         * @since 2024-01-17 避免重复遍历
         */
        $key = "{$i}-{$lowLimit}-{$highLimit}-{$sum}";
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2024-01-17 当前位数字
         */
        $max = $highLimit ? $num2[$i] : 9;
        $min = $lowLimit ? $num1[$i] : 0;

        /**
         * @since 2024-01-17 [i+1, num]的好整数个数
         */
        $goodCount = 0;
        for($j=$min; $j<=$max; $j++){
            $goodCount += $this->GoodCount($num1, $num2, $i+1, $lowLimit && $j==$min, $highLimit && $j==$max, $sum+$j, $minSum, $maxSum);
        }

        /**
         * @return Integer
         */
        return $this->dp[$key] = $goodCount % (pow(10, 9)+7);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.17_2.php
 */
$datas = [
    ["0", "432", 0, 3], // 20
    ["1", "12", 1, 8], // 11
    ["4179205230", "7748704426", 8, 46], // 883045899
    ["1012640017461217236611", "9234552128261772272769", 67, 148], // 683479047
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->count(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
